// @charset "utf-8";
/**
 * 请求行情数据类
 */
define(function (require, exports, module) {
	var Time = require('./time');

	var gHexinApiQuote = '';
	try {
		gHexinApiQuote = external.createObject('Quote');
	} catch (exp) {
		//
	}

	var ClientQuote = function () {
		this.request = function (params, callback) {
			var self = this;
			var req = params;
			req.onready = function() {
				if (16384 == params.period) {
					self.dayResponse(params, callback);
				}

				if (12288 === params.period) {

					self.minLineResponse(params, callback);
				}
				if (0 === params.period) {
					self.minResponse(params, callback);
				}
			};

			try {
				gHexinApiQuote.request(req);
			} catch (exp) {
				//alert(exp.message);
				return;
			}
		};
		this.dayResponse = function (params, callback) {
			var self = this;

			var dataType = params.type.split(',');
			var result = {};


			for (var date = params.begin; date <= params.end; date++) {
				if (date % 100 > 31) {
					continue;
				}
				var row = {};
				for (var type in dataType) {
					if (isNaN(type)) {
						continue;
					}
					var ret = {
						code : params.code,
						type : dataType[type],
						period : params.period,
						time : date
					};

					try {
						var data = gHexinApiQuote.getData(ret);
						if('({})' == data){
							row = false;
							continue;
						}
						data = eval(data);

						if (self.isStringType(ret.type)) {
							row[ret.type] = data[ret.code][ret.type];
						} else {
							if (isNaN(data[ret.code][ret.type])) {
								row[ret.type] = '--';
								continue;
							}
							row[ret.type] = parseFloat(data[ret.code][ret.type]).toFixed(2);
						}
					} catch (exp) {
						continue;
					}
				}

				if (false !== row) {
					result[date] = row;
				}
			}
			callback(result);
		};
		this.minResponse = function (params, callback) {
			var self = this;
			var dataType = params.type.split(',');
			var result = {};

			for (var type in dataType) {
				if (isNaN(type)) {
					continue;
				}
				var ret = {
					code : params.code,
					type : dataType[type]
				};

				try {
					var data = gHexinApiQuote.getData(ret);
					data = eval(data);
					for (var i in data) {
						if (undefined === result[i]) {
							result[i] = {};
						}

						if (self.isStringType(ret.type)) {
							result[i][ret.type] = data[i][ret.type];
						} else {
							if (isNaN(data[i][ret.type])) {
								result[i][ret.type] = '--';
								continue;
							}
							result[i][ret.type] = parseFloat(data[i][ret.type]).toFixed(2);
						}
					}
				} catch (exp) {
					continue;
				}
			}
			callback(result);
		};
		this.minLineResponse = function (params, callback) {
			var result = {};
			var dateDay = params.dateDay;
			var timeArr = [];
			var time = new Time();
			for (var i = 9; i <= 15; i++) {
				if (i === 12) {
					continue;
				}
				var hour = i+'';
				if (i <= 9) {
					hour = '0'+i;
				}

				for (var j = 0; j <= 59; j++) {
					if (i === 9 && j <= 29) {
						continue;
					}
					if (i === 11 && j > 30) {
						continue;
					}
					if (i === 15 && j > 0) {
						continue;
					}
					if (j % 5 !== 0) {
						continue;
					}
					var min = j;
					if (j <= 9) {
						min = '0'+j;
					}
					timeArr.push(time.formatDateTime(dateDay+' '+hour+':'+min, 12288));

				}
			}

			var dataType = [];
			var preTime = '';
			for (var m in timeArr) {
				if (undefined === result[timeArr[m]]) {
					result[timeArr[m]] = {};
				}

				dataType = params.type.split(',');
				for (var type in dataType) {
					var ret = {
						code : params.code,
						type : dataType[type],
						mode : 'beforecalclast',
						period : 12288,
						time : timeArr[m]
					};

					var data = gHexinApiQuote.getData(ret);
					data = eval(data);
					if (undefined === data[params.code]) {
						continue;
					}

					if (dataType[type]  === 'close') {
						if (preTime === data[params.code]['time']) {
							result[timeArr[m]][dataType[type]] = null;
							break;
						} else {
							preTime = data[params.code]['time'];
						}
					}

					result[timeArr[m]][dataType[type]] = data[params.code][dataType[type]];
				}
			}

			var chartData = [];
			var chartRes = {};
			var step = 0;
			var closeSubPre = 0;
			for (var a in timeArr) {
				if (undefined === chartRes['pre']) {
					chartRes['pre'] = parseFloat(result[timeArr[a]]['pre']);
				}

				var close = result[timeArr[a]]['close'] === null ? null : parseFloat(result[timeArr[a]]['close']);
				if (close === null) {
					continue;
				}
				closeSubPre = Math.abs(close-chartRes['pre']);
				if (0 === step) {
					step = closeSubPre;
				}
				if (closeSubPre > step) {
					step = closeSubPre;
				}
				chartData.push(close);
			}

			chartRes['step'] = step;
			chartRes['chartData'] = chartData;
			callback(chartRes);
		};
		this.getStockDayData = function (params, callback) {
			params.period = 16384;
			this.request(params, callback);
		};
		this.getStockMinData = function (params, callback) {
			params.period = 0;
			this.request(params, callback);
		};
		this.formatForCandlestick = function (OHLCData) {
			var result = [];

			for (var date in OHLCData) {
				if (isNaN(date)) {
					continue;
				}
				var row = [];

				var time = date.replace(/(\d{4})(\d{2})(\d{2})/, '$1/$2/$3');
				row.push(Date.parse(time));

				for (var type in OHLCData[date]) {
					if (undefined === OHLCData[date][type]) {
						continue;
					}
					row.push(parseFloat(OHLCData[date][type]));
				}
				result.push(row);
			}

			return result;
		};
		this.isStringType = function (type) {
			if ('zqmc' == type) {
				return true;
			}
			if ('industry' == type) {
				return true;
			}

			return;
		};
	};

	module.exports = new ClientQuote();
});